<?php
namespace app\admin\builder;
class AdminAccessBuilder extends AdminBuilder
{
    private $_title;
    private $_suggest;
    private $_data;
    private $_saveUrl;
    private $_groupId;
    /**
     * 设置页面标题
     * @param $title
     * @return $this
     */
    public function title($title)
    {
        $this->_title = $title;
        $this->meta_title = $title;
        return $this;
    }


    /**
     * 设置页面副标题
     * @param $suggest
     * @return $this
     */
    public function suggest($suggest)
    {
        $this->_suggest = $suggest;
        return $this;
    }

    /**
     * 设置
     * @param $url
     * @return $this
     */
    public function setSaveUrl($url){
        $this->_saveUrl=$url;
       return $this;
    }


    /**
     * 存放某角色权限数据
     * @param $list
     * @return $this
     */
    public function data($list)
    {
        $this->_data = $list;
        return $this;
    }

    public function setGroupId($id){

        $this->_groupId=$id;
        return $this;
    }

    /**
     * 展示页面
     * 数据赋予
     * @return mixed
     */
    public function displayAccess()
    {
        //显示页面
        $this->assign('title', $this->_title);
        $this->assign('suggest', $this->_suggest);
        $data = model('Rule')->getRules();
//        return json($this->returnNodes($data,$this->_data));exit();
        $this->assign('nodes',$this->returnNodes($data,$this->_data));
        $this->assign('savePostUrl',$this->_saveUrl);
        $this->assign('groupId',$this->_groupId);
        return parent::display('admin_access');
    }


    /**
     *
     * @param $nodes
     * @param $groupRules string 1,2,3,4,5
     * @return mixed
     */
    final protected function returnNodes($nodes, $groupRules)
    {
        is_string($groupRules)&& $groupRules=explode(',',$groupRules);
        if(empty($groupRules)){
            return $nodes;
        }
      return   accessRule($nodes,is_array($groupRules)?$groupRules:[]);
    }



}